using System;
using System.Collections.Generic;
using System.Text;
using EntlibAOP.Diagnostic.Tracing;
using EntlibAOP.Diagnostic.Logging;
using EntlibAOP.ExceptionHandling;
using EntlibAOP.Caching;
using System.Diagnostics;
using System.Threading;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using EntlibAOP.Security.Cryptography;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;


namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            tester test = new tester();
         
            #region Benchmarking code
           
            //Stopwatch stopWatch = new Stopwatch();
            //stopWatch.Start();
            //for (int i = 0; i < 1000; i++)
            //{
            //    Console.WriteLine(test.WhatIsMyName("Dhejo ", "Vanissery"));
            //    Console.WriteLine(test.WhatIsMyName2("Dhejo ", "Vanissery"));
            //    Console.WriteLine(test.WhatIsMyName3("Dhejo ", "Vanissery"));
            //    Console.WriteLine(test.WhatIsMyName4("Dhejo ", "Vanissery"));
            //}
            //stopWatch.Stop();

            //TimeSpan ts = stopWatch.Elapsed;
            //    // Format and display the TimeSpan value.
            //string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            //        ts.Hours, ts.Minutes, ts.Seconds,
            //        ts.Milliseconds / 10);
            //Console.WriteLine(elapsedTime);
            
            #endregion

            #region TracingCode
            //Console.WriteLine(test.WhatIsMyName("Dhejo ", "Vanissery"));
            //Console.WriteLine(test.WhatIsMyName2("Deepak ", "Gupta"));
            //Console.WriteLine(test.WhatIsMyName3("Manoj ", "Garg"));
            //Console.WriteLine(test.WhatIsMyName4("Yogesh ", "Mehra"));
            //Console.WriteLine(test.WhatIsMyName4("ITG ", "Team"));
            #endregion

            #region Exception Handling
            //long result = test.Division(20, 0);
            #endregion

            #region Caching Code
            //Console.WriteLine("first Call");
            //Employee testEmployee = test.GetRandomEmployee("Dhejo");
            //Console.WriteLine(" First Name {0} Last Name {1} ID{2}", testEmployee.FirstName, testEmployee.LastName, testEmployee.ID);
            //Thread.Sleep(1000);
            //Console.WriteLine("Second Call");
            //Employee testEmployee2 = test.GetRandomEmployee("Dhejo");
            //Console.WriteLine(" First Name {0} Last Name {1} ID{2}", testEmployee2.FirstName, testEmployee2.LastName, testEmployee2.ID);
            #endregion

            #region Cryptograpy

            Console.WriteLine("String to be encrypted");
            Console.WriteLine("This is secert 1");
            Console.WriteLine("This is secert 2");
            string cipherText = test.SecertData("This is secert 1");
            string cipherText2 = test.SecertData("This is secert 2");

            Console.WriteLine("Encrypted strings");
            Console.WriteLine(cipherText);
            Console.WriteLine(cipherText2);

            Console.WriteLine("Decrypted strings");
            test.SecertHandler(cipherText,cipherText2);
            #endregion

            Console.ReadLine();
        }
    }

    //[PerformanceTracker]
    [TraceApplication(BatchLogging = true, BatchSize = 3, Categories = new string[] { "Log Category" }, 
        TraceExecutionOptions = TraceExecutionEvent.Entry | TraceExecutionEvent.Exit |
        TraceExecutionEvent.Exception | TraceExecutionEvent.Success )]
    
    public class tester
    {
        [Cache("EmployeeKey",MatchParameter=true)]
        public Employee GetRandomEmployee(string name)
        {
            return DataAccess.GetEmployeeRandom();
        }
        public string WhatIsMyName(string firstName, string lastName)
        {
            //Thread.Sleep(1000);
            //Logger.WriteInformation("Entering method WhatIsMyName" +
            //    Environment.NewLine + "Parmeters : firstName = " + firstName + " lastName = " + lastName);
            return "Hello " + firstName + lastName;
        }
        public string WhatIsMyName2(string firstName, string lastName)
        {
                //Logger.WriteInformation("Entering method WhatIsMyName2" +
                //   Environment.NewLine + "Parmeters : firstName = " + firstName + " lastName = " + lastName);

                ////Code to find music is here
                ////This code will match the search parameters with list of songs in DB
                //// It will return the matched songs as a collection type MatchedSong


                //Logger.WriteInformation("Exiting method WhatIsMyName2" +
                //   Environment.NewLine + "Return value " + MatchedSongs);

                 return "Hello " + firstName + lastName;
            
        }
        public string WhatIsMyName3(string firstName, string lastName)
        {
            //Logger.WriteInformation("Entering method WhatIsMyName3" +
            //    Environment.NewLine + "Parmeters : firstName = " + firstName + " lastName = " + lastName);
            return "Hello3 " + firstName + lastName;
        }
        public string WhatIsMyName4(string firstName, string lastName)
        {
            //Logger.WriteInformation("Entering method WhatIsMyName4" +
            //    Environment.NewLine + "Parmeters : firstName = " + firstName + " lastName = " + lastName);
            return "Hello4 " + firstName + lastName;
        }
        public string WhatIsMyName5(string firstName, string lastName)
        {
            //Logger.WriteInformation("Entering method WhatIsMyName5" +
            //    Environment.NewLine + "Parmeters : firstName = " + firstName + " lastName = " + lastName);
            return firstName + lastName;
        }
        //[ExceptionHandler(PolicyName="My Policy")]
        public long Division(int arg1, int arg2)
        {
            long result = 0;
            //try
            //{
            result = arg1 / arg2;
            //}
            //catch(Exception ex)
            //{
            //    bool rethrow = ExceptionPolicy.HandleException
            //       (ex, "My Policy");
            //    //if (rethrow)
            //    //    throw;
            //}
            return result;
        }

        [Encryption(ConfigSettingName="TripleDESCryptoServiceProvider")]
        public string SecertData(string secert)
        {
            return secert;
        }

        [Decryption(ConfigSettingName="TripleDESCryptoServiceProvider", ParameterNames=new string[]{"secert1","secert2"})]
        public void SecertHandler(string secert1, string secert2)
        {
            Console.WriteLine(secert1);            
            Console.WriteLine(secert2);
        }

    }
}
